const Model = require('./index')

class FeedbackModel extends Model {
  /**
   * 分页查询反馈列表
   * @param {*} pageNo
   * @param {*} pageSize
   * @param {*} conditions
   * @returns
   */
  async findPage ({ pageNo, pageSize }) {
    const sql = `select feedback.*, user.nickname from feedback
      left join user on user.id = feedback.userId
      order by feedback.createTime desc
      limit ${pageSize} offset ${(pageNo - 1) * pageSize}`
    try {
      const list = await this.db.query(sql)
      const total = await this.alltotal()
      return {
        list,
        total
      }
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 删除反馈以及反馈下面的表情操作
   * @param {} id
   */
  async delete (id) {
    const sql = `delete feedback, feedback_attitude from feedback
      left join feedback_attitude on feedback_attitude.feedbackId = feedback.id
      where feedback.id = ?`
    try {
      await this.db.query(sql, [id])
    } catch (error) {
      throw new Error(error)
    }
  }
}

module.exports = new FeedbackModel('feedback')
